home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / port / hpux / flock.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  78 lines

  1. /*
  2.  * flock (fd, operation)
  3.  *
  4.  * This routine performs some file locking like the BSD 'flock'
  5.  * on the object described by the int file descriptor 'fd',
  6.  * which must already be open.
  7.  *
  8.  * The operations that are available are:
  9.  *
  10.  * LOCK_SH  -  get a shared lock.
  11.  * LOCK_EX  -  get an exclusive lock.
  12.  * LOCK_NB  -  don't block (must be ORed with LOCK_SH or LOCK_EX).
  13.  * LOCK_UN  -  release a lock.
  14.  *
  15.  * Return value: 0 if lock successful, -1 if failed.
  16.  *
  17.  * Note that whether the locks are enforced or advisory is
  18.  * controlled by the presence or absence of the SETGID bit on
  19.  * the executable.
  20.  *
  21.  * Note that there is no difference between shared and exclusive
  22.  * locks, since the 'lockf' system call in SYSV doesn't make any
  23.  * distinction.
  24.  *
  25.  * The file "<sys/file.h>" should be modified to contain the definitions
  26.  * of the available operations, which must be added manually (see below
  27.  * for the values).
  28.  *
  29.  * This comes from a regular post in comp.sys.hp.  /lars-owe
  30.  */
  31.  
  32. #include <unistd.h>
  33. #include <sys/file.h>
  34. #include <errno.h>
  35.  
  36. #include "port.h"
  37.  
  38. extern int errno;
  39.  
  40.     int
  41. flock (int fd, int operation)
  42. {
  43.     int i;
  44.  
  45.     switch (operation) {
  46.  
  47.     /* LOCK_SH - get a shared lock */
  48.     case LOCK_SH:
  49.     /* LOCK_EX - get an exclusive lock */
  50.     case LOCK_EX:
  51.         i = lockf (fd, F_LOCK, 0);
  52.         break;
  53.  
  54.     /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
  55.     case LOCK_SH|LOCK_NB:
  56.     /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
  57.     case LOCK_EX|LOCK_NB:
  58.         i = lockf (fd, F_TLOCK, 0);
  59.         if (i == -1)
  60.             if ((errno == EAGAIN) || (errno == EACCES))
  61.                 errno = EWOULDBLOCK;
  62.         break;
  63.  
  64.     /* LOCK_UN - unlock */
  65.     case LOCK_UN:
  66.         i = lockf (fd, F_ULOCK, 0);
  67.         break;
  68.  
  69.     /* Default - can't decipher operation */
  70.     default:
  71.         i = -1;
  72.         errno = EINVAL;
  73.         break;
  74.     }
  75.  
  76.     return (i);
  77. }
  78.